/*
 * CadastrodeEstacao.java
 *
 * Created on 26 de Novembro de 2008, 16:22
 */

package br.faimg.pomar.visual;

import br.faimg.pomar.controle.componentevisual.TableEstacao;
import javax.swing.JTable;
import javax.swing.table.TableModel;

/**
 *
 * @author  diogo
 */
public class CadastrodeEstacao extends javax.swing.JInternalFrame {

    /** Creates new form CadastrodeEstacao */
    public CadastrodeEstacao() {
       initComponents();  

       alterarjButton.setEnabled(false);
       
       updateTable();
    }

    
    private void edicaoOUcancelar() {
        cancelarButton.setText("Cancelar");
    }
    
        private void updateTable() {
        //Lista os itens cadastrados no banco na tablela
        tabelajTable.setModel(new JTable(
                new TableEstacao().getTableModel()).getModel());

        //remove a coluna do codigo do produto
        tabelajTable.removeColumn(tabelajTable.getColumn("Código do Tipo de Estacao"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cancelarButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        tabelaScrollPane = new javax.swing.JScrollPane();
        tabelajTable = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        alterarjButton = new javax.swing.JButton();

        setTitle("Cadastro de Estações");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelarButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 350, 90, -1));

        okButton.setText("Incluir");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        jPanel1.add(okButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 350, 90, -1));

        tabelaScrollPane.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaScrollPaneMouseClicked(evt);
            }
        });

        tabelajTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabelajTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelajTableMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tabelajTableMouseReleased(evt);
            }
        });
        tabelaScrollPane.setViewportView(tabelajTable);

        jPanel1.add(tabelaScrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 610, 320));
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, 610, 10));

        alterarjButton.setText("Alterar");
        alterarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButtonActionPerformed(evt);
            }
        });
        jPanel1.add(alterarjButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 350, 90, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 640, 390));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed

   this.setVisible(false);

}//GEN-LAST:event_cancelarButtonActionPerformed

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
    
    TableModel tableTemp = tabelajTable.getModel();
    int linha = tabelajTable.getSelectedRow();
    
    CadastrodeEstacaoInclusao view = new CadastrodeEstacaoInclusao();
    view.setModal(true);
    view.setVisible(true);
    view.setResizable(true);
    
    
    updateTable();
  
    
}//GEN-LAST:event_okButtonActionPerformed

private void tabelajTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelajTableMouseClicked
    if (tabelajTable.getSelectedRow() >= 0){
        alterarjButton.setEnabled(true);
    } else {
        alterarjButton.setEnabled(false);
    }
}//GEN-LAST:event_tabelajTableMouseClicked

private void alterarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButtonActionPerformed
    
    TableModel tableTemp = tabelajTable.getModel();
    int linha = tabelajTable.getSelectedRow();
    
    CadastrodeEstacaoInclusao view = new CadastrodeEstacaoInclusao((String)tableTemp.getValueAt(linha, 0), 
                                                                   (String)tableTemp.getValueAt(linha, 1),
                                                                   (String)tableTemp.getValueAt(linha, 3));
    
    view.setModal(true);
    view.setResizable(true);
    view.setVisible(true);
    
    
    updateTable();
    
            
    
}//GEN-LAST:event_alterarjButtonActionPerformed

private void tabelajTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelajTableMouseReleased

}//GEN-LAST:event_tabelajTableMouseReleased

private void tabelaScrollPaneMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaScrollPaneMouseClicked

}//GEN-LAST:event_tabelaScrollPaneMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton;
    private javax.swing.JButton cancelarButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton okButton;
    private javax.swing.JScrollPane tabelaScrollPane;
    private javax.swing.JTable tabelajTable;
    // End of variables declaration//GEN-END:variables

}
